package com.example.retrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;


import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;



import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    public static String host="http://124.93.196.45:10001";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add();
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        apiService.getNewType().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        // 获取响应体的字符串表示
                        String responseBodyString = response.body().string();

                        // 使用 Gson 解析 JSON 数据
                        JsonParser jsonParser = new JsonParser();
                        JsonObject jsonObject = jsonParser.parse(responseBodyString).getAsJsonObject();

                        // 获取 "data" 数组
                        JsonArray dataArray = jsonObject.getAsJsonArray("data");

                        // 遍历数组
                        for (JsonElement element : dataArray) {
                            JsonObject categoryObject = element.getAsJsonObject();
                            int id = categoryObject.getAsJsonPrimitive("id").getAsInt();
                            String name = categoryObject.getAsJsonPrimitive("name").getAsString();

                            Log.d("新闻分类", "ID: " + id + ", Name: " + name);
                            // 还可以根据需要输出其他字段
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("新闻分类", "请求失败，错误码：" + response.code());
                }
            }



            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("aaaa");
                Log.e("新闻信息", "请求失败", throwable);
                System.out.println("aaaa: " + throwable.getMessage());
            }
        });
    }

    private void add() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        apiService.getNewType1().enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                if (response.isSuccessful()) {
                    GsonBean gsonBean = response.body();
                    if (gsonBean != null && gsonBean.data != null) {
                        for (GsonBean.Data data : gsonBean.data) {
                            Log.d("新闻信息", "Name: " + data.name + ", ID: " + data.id);
                            // 还可以根据需要输出其他字段
                        }
                    } else {
                        Log.e("新闻信息", "响应数据为空");
                    }
                } else {
                    Log.e("新闻信息", "请求失败，错误码：" + response.code());
                }
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {
                System.out.println("aaaa");
                Log.e("新闻信息", "请求失败", throwable);
                System.out.println("aaaa: " + throwable.getMessage());
            }
        });
    }
}